﻿using System;
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Sovigaz.DataProvider;
using Sovigaz.Permission.Repositories;
using Utilities;

namespace Sovigaz.Permission.Api
{
    public class IndemnifyProductApiController : ApiController
    {
        public IndemnifyProductRepository Get(int? pageNumber, string orderBy, string orderDirection, string indemnifyProductCode, string partnerID, string datefrom, string dateto)
        {
            try
            {
                string whereClause = "IsDeleted != true";
                if (!String.IsNullOrWhiteSpace(indemnifyProductCode))
                    whereClause = String.Format(whereClause + " AND indemnifyProductCode.Contains(\"{0}\")", indemnifyProductCode);
                if (!String.IsNullOrWhiteSpace(partnerID) && ConvertUtility.IntParse(partnerID) > 0)
                    whereClause = String.Format(whereClause + " AND partnerID = {0}", partnerID);
                if (datefrom.Contains("NaN") || dateto.Contains("NaN")) return new IndemnifyProductRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1);
                return new IndemnifyProductRepository().GetList(whereClause, orderBy, orderDirection, pageNumber ?? 1, ConvertUtility.DateTimeParse(datefrom), ConvertUtility.DateTimeParse(dateto));
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public IndemnifyProduct Get(int? id)
        {
            try
            {
                var item = new IndemnifyProduct();
                var indemnifyProductRepository = new IndemnifyProductRepository();
                item.IndemnifyProductCode = GenerateCode.GetCode(indemnifyProductRepository.GetNextIndex(), CodeType.IdemnifyProduct);
                if (id.HasValue && id > 0)
                {
                    item = indemnifyProductRepository.GetByID(id.Value);
                    if (item.IndemnifyProductCode == string.Empty) item.IndemnifyProductCode = GenerateCode.GetCode(indemnifyProductRepository.GetNextIndex(), CodeType.IdemnifyProduct);
                }
                return item;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi truy xuất dữ liệu.", e);
            }
        }

        public void Delete(int? id)
        {
            try
            {
                if (id.HasValue && id > 0)
                {
                    var repository = new DeliveryPlanRepository();
                    repository.Delete(id.Value);
                }
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }



        public IndemnifyProductRepository Delete(string ids, int? pageNumber, string orderBy, string orderDirection, string indemnifyProductCode, string partnerID, string datefrom, string dateto)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');
                var repository = new IndemnifyProductRepository();
                repository.Delete(idArr);
                return Get(pageNumber, orderBy, orderDirection, indemnifyProductCode, partnerID, datefrom, dateto);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public void Head(string ids, string status)
        {
            try
            {
                if (ids.StartsWith(","))
                    ids = ids.Remove(0, 1);
                var idArr = ids.Split(',');
                var repository = new IndemnifyProductRepository();
                repository.UpdateStatus(idArr, status);
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException("Có lỗi xảy ra khi xóa dữ liệu.", e);
            }
        }

        public IndemnifyProduct Put(IndemnifyProduct item)
        {
            try
            {
                var repository = new IndemnifyProductRepository();
                int ID = repository.Update(item);
                return Get(ID);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw ExceptionHelper.CreateHttpResponseException(e.Message, e);
            }
        }
    }
}
